<template>
  <div class="app-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <div class="header-left">
            <span class="title">家族捐赠榜</span>
          </div>
          <div class="header-right">
            <el-radio-group v-model="timeFilter">
              <el-radio-button label="all">全部</el-radio-button>
              <el-radio-button label="year">年度</el-radio-button>
              <el-radio-button label="month">本月</el-radio-button>
            </el-radio-group>
          </div>
        </div>
      </template>

      <el-tabs v-model="activeTab">
        <el-tab-pane label="捐赠排行榜" name="1">
          <el-row :gutter="[16, 16]">
            <!-- 前三名特别展示 -->
            <el-col :xs="24" :lg="24">
              <div class="top-three">
                <div v-for="(item, index) in topThree" :key="item.id" class="top-rank-item" :class="`rank-${index + 1}`">
                  <div class="medal-container">
                    <el-image :src="getRankIcon(index)" :alt="`第${index + 1}名`" class="medal-icon" />
                  </div>
                  <el-avatar :size="index === 0 ? 100 : 80" :src="item.avatar" class="top-avatar" />
                  <div class="top-info">
                    <div class="top-name">{{ item.name }}</div>
                    <div class="top-amount">¥ {{ item.amount }}</div>
                    <el-tag :type="getHonorTagType(index)" effect="dark" class="top-honor">{{ item.honor }}</el-tag>
                  </div>
                </div>
              </div>
            </el-col>
            
            <!-- 排行榜列表 -->
            <el-col :xs="24" :lg="16">
              <el-card class="rank-list-card" :bordered="false">
                <template #header>
                  <div class="card-header">
                    <span>捐赠排行</span>
                  </div>
                </template>
                <el-table :data="otherDonations" style="width: 100%" border>
                  <el-table-column label="排名" width="80">
                    <template #default="{ $index }">
                      <span class="rank-number">{{ $index + 4 }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="捐赠人" width="200">
                    <template #default="{ row }">
                      <div class="donor-info">
                        <el-avatar :size="40" :src="row.avatar" />
                        <span class="donor-name">{{ row.name }}</span>
                      </div>
                    </template>
                  </el-table-column>
                  <el-table-column prop="amount" label="捐赠金额" width="150">
                    <template #default="{ row }">
                      <span class="donation-amount">¥ {{ row.amount }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="荣誉称号" width="120">
                    <template #default="{ row, $index }">
                      <el-tag :type="getHonorTagType($index + 3)">{{ row.honor }}</el-tag>
                    </template>
                  </el-table-column>
                  <el-table-column label="操作" width="100" fixed="right">
                    <template #default="{ row }">
                      <el-button type="primary" link @click="showDonorDetail(row)">详情</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-card>
            </el-col>
            
            <!-- 统计信息 -->
            <el-col :xs="24" :lg="8">
              <el-card class="stats-card" :bordered="false">
                <template #header>
                  <div class="card-header">
                    <span>捐赠统计</span>
                  </div>
                </template>
                <div class="stats-container">
                  <div class="stat-item">
                    <div class="stat-icon">
                      <el-icon><User /></el-icon>
                    </div>
                    <div class="stat-info">
                      <div class="stat-value">{{ totalDonors }}</div>
                      <div class="stat-label">捐赠人数</div>
                    </div>
                  </div>
                  <div class="stat-item">
                    <div class="stat-icon">
                      <el-icon><Money /></el-icon>
                    </div>
                    <div class="stat-info">
                      <div class="stat-value">¥ {{ totalAmount }}</div>
                      <div class="stat-label">捐赠总额</div>
                    </div>
                  </div>
                  <div class="stat-item">
                    <div class="stat-icon">
                      <el-icon><TrendCharts /></el-icon>
                    </div>
                    <div class="stat-info">
                      <div class="stat-value">{{ avgAmount }}</div>
                      <div class="stat-label">人均捐赠</div>
                    </div>
                  </div>
                </div>
                
                <el-divider />
                
                <h4>捐赠用途分布</h4>
                <div class="chart-container" ref="chartContainer"></div>
              </el-card>
            </el-col>
          </el-row>
        </el-tab-pane>

        <el-tab-pane label="荣誉墙" name="2">
          <div class="honor-filter">
            <el-select v-model="honorFilter" placeholder="按荣誉等级筛选" style="width: 200px">
              <el-option label="全部荣誉" value="" />
              <el-option label="永久荣誉" value="永久荣誉" />
              <el-option label="特级功德" value="特级功德" />
              <el-option label="一级功德" value="一级功德" />
            </el-select>
          </div>
          
          <el-row :gutter="[16, 16]">
            <el-col v-for="donor in filteredHonorWall" :key="donor.id" :xs="24" :sm="12" :md="8" :lg="6">
              <el-card class="honor-card" shadow="hover">
                <div class="honor-image-container">
                  <el-image :src="donor.image" :alt="donor.name" class="honor-image" />
                  <div class="honor-badge" :class="getHonorBadgeClass(donor.honorTitle)">
                    {{ donor.honorTitle }}
                  </div>
                </div>
                <div style="padding: 14px;">
                  <h3>{{ donor.name }}</h3>
                  <div class="honor-desc">
                    <p>累计捐赠：<span class="highlight">¥ {{ donor.totalAmount }}</span></p>
                    <p>捐赠次数：<span>{{ donor.donationCount || '1' }}次</span></p>
                    <p>{{ donor.description }}</p>
                  </div>
                  <div class="honor-actions">
                    <el-button type="primary" link @click="showDonorStory(donor)">查看故事</el-button>
                    <el-button type="primary" link @click="showDonationHistory(donor)">捐赠历史</el-button>
                  </div>
                </div>
              </el-card>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    
    <!-- 捐赠者详情弹窗 -->
    <el-dialog
      v-model="donorDetailVisible"
      :title="`${currentDonor?.name || ''}的捐赠详情`"
      width="500px"
      destroy-on-close
    >
      <div v-if="currentDonor" class="donor-detail">
        <div class="donor-profile">
          <el-avatar :size="80" :src="currentDonor.avatar" />
          <div class="donor-profile-info">
            <h3>{{ currentDonor.name }}</h3>
            <p>累计捐赠: ¥ {{ currentDonor.totalAmount || currentDonor.amount }}</p>
            <p>荣誉称号: {{ currentDonor.honorTitle || currentDonor.honor }}</p>
          </div>
        </div>
        <el-divider />
        <h4>捐赠记录</h4>
        <el-timeline>
          <el-timeline-item
            v-for="(record, index) in donorHistory"
            :key="index"
            :timestamp="record.date"
            placement="top"
          >
            <h4>¥ {{ record.amount }}</h4>
            <p>用途: {{ record.purpose }}</p>
          </el-timeline-item>
        </el-timeline>
      </div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { User, Money, TrendCharts } from '@element-plus/icons-vue'

interface DonorRecord {
  id: number
  name: string
  amount: string
  totalAmount?: string
  honor: string
  avatar: string
  honorTitle?: string
  image?: string
  donationCount?: number
  description?: string
  story?: string
}

interface DonationRecord {
  date: string
  amount: string
  purpose: string
}

// 状态变量
const activeTab = ref('1')
const timeFilter = ref('all')
const honorFilter = ref('')
const donorDetailVisible = ref(false)
const currentDonor = ref<DonorRecord | null>(null)
const donorHistory = ref<DonationRecord[]>([])

// 排名图标
const getRankIcon = (index: number) => {
  const icons = [
    '/assets/rank/gold.png',
    '/assets/rank/silver.png',
    '/assets/rank/bronze.png'
  ]
  return icons[index]
}

// 获取荣誉标签类型
const getHonorTagType = (index: number) => {
  const types = ['danger', 'warning', 'success', 'info', 'info']
  return types[index] || 'info'
}

// 获取荣誉徽章样式
const getHonorBadgeClass = (honorTitle: string) => {
  const classMap: Record<string, string> = {
    '永久荣誉理事': 'badge-gold',
    '特级功德理事': 'badge-silver',
    '一级功德理事': 'badge-bronze',
  }
  return classMap[honorTitle] || 'badge-default'
}

// 年度捐赠数据
const yearlyDonations = [
  {
    id: 1,
    name: '张大善',
    amount: '100,000',
    totalAmount: '500,000',
    honor: '永久荣誉',
    avatar: '/assets/avatars/1.png'
  },
  {
    id: 2,
    name: '李善缘',
    amount: '80,000',
    totalAmount: '300,000',
    honor: '特级功德',
    avatar: '/assets/avatars/2.png'
  },
  {
    id: 3,
    name: '王积德',
    amount: '50,000',
    totalAmount: '150,000',
    honor: '一级功德',
    avatar: '/assets/avatars/3.png'
  },
  {
    id: 4,
    name: '赵明德',
    amount: '30,000',
    totalAmount: '100,000',
    honor: '二级功德',
    avatar: '/assets/avatars/4.png'
  },
  {
    id: 5,
    name: '钱进财',
    amount: '20,000',
    totalAmount: '80,000',
    honor: '二级功德',
    avatar: '/assets/avatars/5.png'
  },
  {
    id: 6,
    name: '孙仁义',
    amount: '15,000',
    totalAmount: '60,000',
    honor: '三级功德',
    avatar: '/assets/avatars/6.png'
  },
  {
    id: 7,
    name: '周家和',
    amount: '10,000',
    totalAmount: '50,000',
    honor: '三级功德',
    avatar: '/assets/avatars/7.png'
  },
  {
    id: 8,
    name: '吴善行',
    amount: '8,000',
    totalAmount: '40,000',
    honor: '四级功德',
    avatar: '/assets/avatars/8.png'
  }
]

// 荣誉墙数据
const honorWall = [
  {
    id: 1,
    name: '张大善',
    image: '/assets/donors/1.jpg',
    avatar: '/assets/avatars/1.png',
    totalAmount: '500,000',
    donationCount: 8,
    honorTitle: '永久荣誉理事',
    description: '多年来持续支持家族发展，为三修基金做出重大贡献。',
    story: '张大善先生自幼深受家族文化熏陶，毕业后创办实业，事业有成后不忘回馈家族...'
  },
  {
    id: 2,
    name: '李善缘',
    image: '/assets/donors/2.jpg',
    avatar: '/assets/avatars/2.png',
    totalAmount: '300,000',
    donationCount: 5,
    honorTitle: '特级功德理事',
    description: '积极参与家族事务，热心公益，贡献突出。',
    story: '李善缘女士作为家族新一代成员，毕业于名校，现为知名企业高管...'
  },
  {
    id: 3,
    name: '王积德',
    image: '/assets/donors/3.jpg',
    avatar: '/assets/avatars/3.png',
    totalAmount: '150,000',
    donationCount: 3,
    honorTitle: '一级功德理事',
    description: '热心家族公益事业，多次参与族谱修编工作。',
    story: '王积德先生是家族中德高望重的长辈，曾担任族谱编纂委员会主任...'
  },
  {
    id: 4,
    name: '赵明德',
    image: '/assets/donors/4.jpg',
    avatar: '/assets/avatars/4.png',
    totalAmount: '100,000',
    donationCount: 2,
    honorTitle: '一级功德理事',
    description: '关心家族发展，积极支持宗祠修缮。',
    story: '赵明德先生虽旅居海外多年，但心系家族，每年都会回乡参加重要家族活动...'
  }
]

// 计算属性
const topThree = computed(() => yearlyDonations.slice(0, 3))
const otherDonations = computed(() => yearlyDonations.slice(3))
const filteredHonorWall = computed(() => {
  if (!honorFilter.value) return honorWall
  return honorWall.filter(donor => donor.honorTitle.includes(honorFilter.value))
})

// 统计数据
const totalDonors = ref(156)
const totalAmount = ref('813,000')
const avgAmount = ref('5,211')

// 方法
const showDonorDetail = (donor: any) => {
  currentDonor.value = donor
  // 模拟捐赠历史数据
  donorHistory.value = [
    { date: '2024-03-15', amount: donor.amount, purpose: '宗祠修缮' },
    { date: '2023-12-20', amount: Math.floor(parseInt(donor.amount.replace(/,/g, '')) * 0.3).toLocaleString(), purpose: '族谱修编' },
    { date: '2023-08-08', amount: Math.floor(parseInt(donor.amount.replace(/,/g, '')) * 0.2).toLocaleString(), purpose: '族规修订' }
  ]
  donorDetailVisible.value = true
}

const showDonorStory = (donor: any) => {
  currentDonor.value = donor
  // 这里可以显示捐赠者故事的弹窗
  // 简化处理，复用详情弹窗
  donorHistory.value = []
  donorDetailVisible.value = true
}

const showDonationHistory = (donor: any) => {
  currentDonor.value = donor
  // 模拟捐赠历史数据
  donorHistory.value = [
    { date: '2024-03-15', amount: '50,000', purpose: '宗祠修缮' },
    { date: '2023-12-20', amount: '30,000', purpose: '族谱修编' },
    { date: '2023-08-08', amount: '20,000', purpose: '族规修订' },
    { date: '2022-10-10', amount: '15,000', purpose: '宗祠修缮' }
  ]
  donorDetailVisible.value = true
}

// 图表相关代码可以在onMounted中添加
// 这里简化处理，实际项目中可以使用echarts等图表库
onMounted(() => {
  // 初始化图表
  // 如果需要使用echarts等图表库，可以在这里初始化
})
</script>

<style scoped>
.app-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 84px);
}

.box-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}

.header-left {
  display: flex;
  align-items: center;
}

.title {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
}

.top-three {
  display: flex;
  justify-content: space-around;
  align-items: flex-end;
  padding: 20px 0;
  background: linear-gradient(to right, #f5f5f5, #e8f4ff, #f5f5f5);
  border-radius: 8px;
  margin-bottom: 24px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.top-rank-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  padding: 20px;
}

.rank-1 {
  order: 2;
  transform: scale(1.1);
}

.rank-2 {
  order: 1;
}

.rank-3 {
  order: 3;
}

.medal-container {
  position: absolute;
  top: -10px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1;
}

.medal-icon {
  width: 40px;
  height: 40px;
}

.top-avatar {
  margin: 20px 0 10px;
  border: 3px solid #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

.top-info {
  text-align: center;
}

.top-name {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 5px;
}

.top-amount {
  font-size: 20px;
  font-weight: bold;
  color: #f5222d;
  margin-bottom: 5px;
}

.top-honor {
  font-size: 12px;
}

.rank-item {
  display: flex;
  align-items: center;
}

.rank-number {
  font-size: 18px;
  font-weight: bold;
  text-align: center;
}

.donor-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.donor-name {
  font-size: 16px;
  font-weight: 500;
}

.donation-amount {
  font-size: 18px;
  font-weight: bold;
  color: #f5222d;
}

.stats-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px;
  border-radius: 4px;
  background-color: #f9f9f9;
}

.stat-icon {
  font-size: 24px;
  color: #1890ff;
}

.stat-value {
  font-size: 20px;
  font-weight: bold;
  color: #1890ff;
}

.stat-label {
  font-size: 14px;
  color: #666;
}

.chart-container {
  height: 200px;
  margin-top: 16px;
}

.honor-filter {
  margin-bottom: 24px;
  text-align: right;
}

.honor-card {
  height: 100%;
  transition: all 0.3s;
}

.honor-card:hover {
  transform: translateY(-5px);
}

.honor-image-container {
  position: relative;
  overflow: hidden;
}

.honor-image {
  height: 200px;
  object-fit: cover;
  transition: all 0.3s;
}

.honor-card:hover .honor-image {
  transform: scale(1.05);
}

.honor-badge {
  position: absolute;
  top: 10px;
  right: 0;
  padding: 4px 10px;
  color: white;
  font-size: 12px;
  font-weight: bold;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
}

.badge-gold {
  background-color: #faad14;
}

.badge-silver {
  background-color: #8c8c8c;
}

.badge-bronze {
  background-color: #ad6800;
}

.badge-default {
  background-color: #1890ff;
}

.honor-desc {
  margin-top: 12px;
}

.honor-desc p {
  margin-bottom: 4px;
  line-height: 1.5;
}

.highlight {
  color: #f5222d;
  font-weight: bold;
}

.donor-detail {
  padding: 10px;
}

.donor-profile {
  display: flex;
  gap: 20px;
  align-items: center;
  margin-bottom: 20px;
}

.donor-profile-info h3 {
  margin-top: 0;
  margin-bottom: 8px;
}

.donor-profile-info p {
  margin: 4px 0;
}

.honor-actions {
  margin-top: 16px;
  display: flex;
  justify-content: flex-end;
  gap: 16px;
}

.el-timeline {
  margin-top: 20px;
}

.el-timeline-item h4 {
  color: #409EFF;
  font-weight: bold;
}
</style> 